<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Tag;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class TagController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        $lists = Tag::orderBy('hot','desc')->orderBy('id','asc')->paginage(10);
        return view('tag.index')->with("lists",$lists)->with("actions",$this->actions);
    }
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = Tag::orderBy('hot', 'desc')->orderBy('id', 'asc');
        if (isset($data['name']) && $data['name'] != '') {
            $sql->where('name', 'like', '%' . trim($data['name']) . '%');
        }

        if (isset($data['hot']) && $data['hot'] != '') {
            $sql->where('hot', $data['hot']);
        }

        $lists = $sql->paginate(10);

        return view('tag.search')->with("lists", $lists)->with("name", trim($data['name']))
            ->with("hot", $data['hot'])->with("actions", $this->actions);
    }
    public function postMassHot(Request $request){
        $data = $request->input();
        if($data =['hot'] == ''){
            return AjaxCallbackMessage('请选择是否热门',false);
        }
        if($data['tags']==''){
            return AjaxCallbackMessage('请选择你要设置的关键字',false);
        }
        $tags = explode(',',$data['tags']);
        foreach($tags as $v){
            Tag::where('id',$v)->update(['hot'=>$data['hot']]);
        }
        return AjaxCallbackMessage('设置成功',true);
    }
}
